package com.gxar.quick.ar.server.base.reference;

import com.gxar.common.entity.PageResult;
import com.gxar.common.exception.APIRuntimeException;
import com.gxar.quick.ar.server.base.model.ResultCodeEnum;
import com.gxar.uaa.api.client.IMenuClient;
import com.gxar.uaa.api.client.IRoleClient;
import com.gxar.uaa.api.dto.user.SysUserId;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.rpc.RpcException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author linsy
 * @version 1.0
 * @created 2024/2/27 09:38
 */
@Slf4j
@Service
public class UaaReference {

    @DubboReference(version = "1.0.0")
    private IMenuClient iMenuClient;
    @DubboReference(version = "1.0.0")
    private IRoleClient iRoleClient;

    /**
     * 查询菜单用户
     *
     * @param pageNum   当前页
     * @param pageSize  页记录数
     * @param url       url
     * @param urlMethod url方法
     * @return 响应
     */
    public PageResult<SysUserId> findUserByMenuUrl(Integer pageNum, Integer pageSize, String url, String urlMethod) {
        try {
            com.gxar.uaa.api.dto.PageResult<SysUserId> menuUserPageResult = iMenuClient.findUserByMenuUrl(pageNum, pageSize, url, urlMethod);
            return new PageResult<>(menuUserPageResult.getPageNum(), menuUserPageResult.getPageSize(), menuUserPageResult.getTotalSize(), menuUserPageResult.getContent());
        } catch (RpcException e) {
            // 查询菜单用户失败
            log.info("查询菜单用户失败:[{}]", e.getMessage(), e);
            throw new APIRuntimeException(ResultCodeEnum.MENU_USER_ERROR);
        }
    }

    /**
     * 批量删除
     *
     * @param userId 要删除的用户id集合
     */
    public void batchDelete(List<Long> userId) {
        try {
            iRoleClient.deleteUser(userId);
        } catch (RpcException e) {
            throw new APIRuntimeException(ResultCodeEnum.ROLE_USER_DELETE_ERROR);
        }
    }

}
